package com.example.zhengchaojun.arcgis.basicesTest;

import android.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.data.Feature;
import com.esri.arcgisruntime.data.FeatureTable;
import com.esri.arcgisruntime.geometry.GeometryType;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.view.SketchCreationMode;
import com.esri.arcgisruntime.mapping.view.SketchEditor;
import com.esri.arcgisruntime.mapping.view.SketchStyle;
import com.example.zhengchaojun.arcgis.R;
import com.example.zhengchaojun.arcgis.adapter.FieldAdapter;
import com.example.zhengchaojun.arcgis.basicesTest.adapter.LeandListviewAdapter;
import com.example.zhengchaojun.arcgis.listener.MapSelectOnTouchListener;

/**
 * 类描述：要素编辑（删除，修改，查看）
 * 作者：郑朝军 on 2018/6/6
 * 邮箱：1250393285@qq.com
 * 公司：武汉智博创享科技有限公司
 * 修改人：郑朝军 on 2018/6/6
 * 修改备注：
 */
public class FeaturyEditActivity extends BaseMapActivity
{
    private ListView m_listView;

    private SketchEditor m_sketchEditor;// 草图编辑器
    private SketchStyle m_sketchStyle;// 草图编辑风格
    private int selectItem;
    private MapSelectOnTouchListener m_mapSelectOnTouchListener;

    public int getLayoutID()
    {
        return R.layout.activity_featury_edit;
    }

    @Override
    public void initView()
    {
        super.initView();
        m_listView = findViewById(R.id.listView);
        m_mapViewDelegate.updateMagnifierWihtPanMapEnabled(true);
    }

    @Override
    public void initData()
    {
        m_mapSelectOnTouchListener = new MapSelectOnTouchListener(this, m_mapView);
        m_mapView.setOnTouchListener(m_mapSelectOnTouchListener);

        ArcGISMap arcGISMap = m_mapView.getMap();

        m_sketchEditor = new SketchEditor();
        m_sketchStyle = new SketchStyle();
        m_sketchEditor.setSketchStyle(m_sketchStyle);
        m_mapView.setSketchEditor(m_sketchEditor);

        LeandListviewAdapter adapter3 = new LeandListviewAdapter(this, arcGISMap.getOperationalLayers());
        m_listView.setAdapter(adapter3);
    }

    @Override
    public void initListener()
    {
        m_listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                showShort("开始编辑");
                FeatureLayer layer = (FeatureLayer) ((LeandListviewAdapter) parent.getAdapter()).getItem(position);
                startDraw(layer.getFeatureTable().getGeometryType());
                selectItem = position;
            }
        });
    }

    public void onClick(View view)
    {
        switch (view.getId())
        {
            case R.id.cleardraw:// 重绘
                doClickClearDraw();
                break;
            case R.id.back:// 回退
                doClickBack();
                break;
            case R.id.att:// 属性
                doClickAtt();
                break;
            case R.id.btn_save:// 保存
                doClickEditSave();
                break;
        }
    }

    private void doClickClearDraw()
    {
        FeatureLayer featureLayer = m_mapSelectOnTouchListener.getSelectFeatureLayer();
        if (featureLayer == null)
        {
            return;
        }

        GeometryType geometryType = featureLayer.getFeatureTable().getGeometryType();
        switch (geometryType)
        {
            case POINT:
            case MULTIPOINT:
                m_sketchEditor.start(SketchCreationMode.POINT);
                break;
            case POLYLINE:
                m_sketchEditor.start(SketchCreationMode.POLYLINE);
                break;
            case POLYGON:
            case ENVELOPE:
                m_sketchEditor.start(SketchCreationMode.POLYGON);
                break;
            case UNKNOWN:
                showShort("当前图层：" + featureLayer.getName() + ",数据类型未知");
                break;
        }
        showShort("点击屏幕开始重新绘制要素");
    }


    private void doClickBack()
    {
        if (m_sketchEditor.canUndo())
        {
            m_sketchEditor.undo();
        }
        else
        {
            showShort("当前状态已经无法回退了");
        }
    }

    /**
     * 属性
     */
    private void doClickAtt()
    {
        final Feature feature = m_mapSelectOnTouchListener.getSelectFeature();
        if (feature == null)
        {
            return;
        }

        final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        View view = LayoutInflater.from(this).inflate(R.layout.widget_view_feature_edit_alert_attribute, null);
        ListView listView = view.findViewById(R.id.widget_view_feature_edit_alert_attribute_lvAttribute);
        FieldAdapter fieldAdapter = new FieldAdapter(this, feature);
        listView.setAdapter(fieldAdapter);

        Button btnOK = view.findViewById(R.id.widget_view_feature_edit_alert_attribute_btnOK);
        btnOK.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                FeatureLayer featureLayer = m_mapSelectOnTouchListener.getSelectFeatureLayer();
                FeatureTable featureTable = featureLayer.getFeatureTable();
                if (featureTable.canUpdate(feature))
                {
                    ListenableFuture<Void> listenableFuture = featureTable.updateFeatureAsync(feature);
                    listenableFuture.addDoneListener(new Runnable()
                    {
                        @Override
                        public void run()
                        {
                            showShort("属性保存成功");
                            m_sketchEditor.clearGeometry();
                        }
                    });
                }
                else
                {
                    showShort("要素编辑失败，图层：" + featureLayer.getName() + "不支持编辑");
                }
                alertDialog.dismiss();
            }
        });

        Button btnCancle = view.findViewById(R.id.widget_view_feature_edit_alert_attribute_btnCancle);
        btnCancle.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                alertDialog.dismiss();
            }
        });

        alertDialog.setView(view);
        alertDialog.show();
    }

    private void doClickEditSave()
    {
        FeatureLayer featureLayer = (FeatureLayer) m_listView.getAdapter().getItem(selectItem);
        FeatureTable featureTable = featureLayer.getFeatureTable();
        if (featureTable.canAdd())
        {
            Feature feature = featureTable.createFeature();
            feature.setGeometry(m_sketchEditor.getGeometry());
            ListenableFuture<Void> addFeatureFuture = featureTable.addFeatureAsync(feature);
            addFeatureFuture.addDoneListener(new Runnable()
            {
                @Override
                public void run()
                {
                    showShort("添加成功");
                    m_sketchEditor.clearGeometry();
                }
            });
        }
        else
        {
            showShort("要素添加失败，图层：" + featureLayer.getName() + "不支持编辑");
        }
    }


    private void startDraw(GeometryType geometryType)
    {
        switch (geometryType)
        {
            case POINT:
            case MULTIPOINT:
                m_sketchEditor.start(SketchCreationMode.POINT);
                break;
            case POLYLINE:
                m_sketchEditor.start(SketchCreationMode.POLYLINE);
                break;
            case POLYGON:
            case ENVELOPE:
                m_sketchEditor.start(SketchCreationMode.POLYGON);
                break;
            case UNKNOWN:
                showShort("当前图层：数据类型未知");
                break;
        }
    }
}
